/*
   FieldVis - A Tool for Visualizing Astrophysical Magnetohydrodynamic Flow
   
   Copyright (C) 2006 - 2008     FieldVis Development Team
  
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
  
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
   File     : Texture.h
   Author   : Blayne Field
   
   Purpose  : Definition header file for the Texture class.  This class holds
              all of the relevant information for creating a texture.
*/

#include "Texture.h"


Texture::Texture(std::string pFileName):
fileName(pFileName), minSize(0.01), maxSize(0.1)
{
   ReadTexture();

   glGenTextures(1, &texID);

   //bind textures
   glBindTexture(GL_TEXTURE_2D, texID);
   gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, texWidth, texHeight,
                     GL_RGBA, GL_UNSIGNED_BYTE, texImage);
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

// after loading texture into OpenGL we don't need the byte data anymore...
   free(texImage);
}


bool Texture::ReadTexture()
{
   //reads in texture files in .ppm format
   FILE *infile;
   //ifstream inFile(fileName);
   char cStr[MAX_CHARS];
   int i, j;
   int imgMax;
   long imgSize;
   int r, g, b;
   int c;

   /* open file containing texture */  
   if ((infile = fopen(fileName.c_str(), "r")) == NULL)
   {
      fprintf(stderr, "error opening texture file %s\n", fileName.c_str());
      return false; 
//exit(0);
   }
   //printf("opened file %s\n",filename);
   /* read and discard first two lines */
   fgets(cStr, MAX_CHARS, infile);
   fgets(cStr, MAX_CHARS, infile);

   /* read image size and (?)max component value */
   fscanf(infile, "%d %d", &texWidth, &texHeight);
   fscanf(infile, "%d", &imgMax);
   if (imgMax != 255)
   {
      fprintf(stderr, "error in texture coord\n");
      return false;
      //exit(0);
   }
   /* allocate texture array */
   texLength = texWidth * texHeight;
   imgSize = 4*texLength;
   //printf("img_size: %d\n", img_size);
   if ((texImage = 
        (GLubyte *) calloc(imgSize, sizeof(GLubyte))) == NULL)
   {
      fprintf(stderr, "error with texture\n");   
      return false;
      //exit(0);
   }

   //printf("tex_image: %p\n", tex_image);

   /* read image data */
   //printf("reading data\n");
   c = 0;
   for (i=0; i < texHeight; i++)
   {
      for (j=0; j < texWidth; j++)
      {
         fscanf(infile, "%d %d %d", &r, &g, &b);
         texImage[c++] = (GLubyte) r;
         texImage[c++] = (GLubyte) g;
         texImage[c++] = (GLubyte) b;
         texImage[c++] = (GLubyte) 255;
      }
   }
   //printf("finished reading\n");
   fclose(infile); 

   return true;
}

